using BaseGrpcEFCore.Entity;
using BaseGrpcProtocol;
using BaseGrpcServer.Repository;
using Grpc.Core;

namespace BaseGrpcServer.Services
{
    public class UserService : UserGrpc.UserGrpcBase
    {
        private readonly IUserRepository _userRepository;
        public UserService(BaseDbContext baseDbContext)
        {
            _userRepository = new UserRepository(baseDbContext);
        }
        public override Task<AddUserResp> CraeteUser(AddUserReq request, ServerCallContext context)
        {
            var user = new User();
            user.Name = request.User.Name;
            user.Account = request.User.Account;
            user.Password = request.User.Password;
            user.Email = request.User.Email;
            user.Role = request.User.Role;

            var result = _userRepository.Create(user);
            return Task.FromResult(new AddUserResp()
            {
                CommonResp = new CommonResp() { ErrCode = result }
            });
        }
        public override Task<DeleteUsersResp> DeleteUsers(DeleteUsersReq request, ServerCallContext context)
        {
            var ids = string.Join(",", request.DeleteUserIDList.ToList());
            var result = _userRepository.MultiDelete(ids);
            return Task.FromResult(new DeleteUsersResp()
            {
                CommonResp = new CommonResp() { ErrCode = result }
            });
        }
        public override Task<UpdateUserResp> UpdateUser(UpdateUserReq request, ServerCallContext context)
        {
            var user = new User();
            user.Name = request.UserInfo.Name;
            user.Account = request.UserInfo.Account;
            user.Email = request.UserInfo.Email;
            user.Role = request.UserInfo.Role;
            long id;
            long.TryParse(request.UserInfo.UserId, out id);
            var result = _userRepository.Update(id, user);
            return Task.FromResult(new UpdateUserResp()
            {
                CommonResp = new CommonResp() { ErrCode = result }
            });
        }
        public override Task<GetUserInfoResp> GetUsers(GetUserInfoReq request, ServerCallContext context)
        {
            var idList = request.UserIDList.ToList();
            IEnumerable<User> users;
            if (idList != null && idList.Count > 0)
            {
                users = _userRepository.GetAll(x => idList.Contains(x.Id.ToString()));
            }
            else
            {
                users = _userRepository.GetAll();
            }
            var resp = new GetUserInfoResp();
            foreach (var u in users)
            {
                //the fields in UserInfo must be not null
                resp.UserInfoList.Add(new UserInfo
                {
                    Name = u.Name ?? "",
                    Account = u.Account ?? "",
                    Email = u.Email ?? "",
                    Role = u.Role ?? "",
                    UserId = u.Id.ToString()
                });
            }
            resp.CommonResp = new CommonResp() { ErrCode = 0 };
            return Task.FromResult(resp);
        }
    }
}